package com.cn.aam.checaiduo.ui.me;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.baoyz.widget.PullRefreshLayout;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListV2Listener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.base.adapter.decoration.DividerItemDecoration;
import com.cn.aam.checaiduo.bean.BannerB;
import com.cn.aam.checaiduo.bean.base.PageB;
import com.cn.aam.checaiduo.ui.me.adapter.PlatformIntroduceAdapter;
import com.cn.aam.checaiduo.widget.MultiStateView;

import java.util.List;

import butterknife.BindView;

import static android.support.v7.widget.LinearLayoutManager.VERTICAL;

/**
 * monker on 2017/6/28.
 * 平台介绍和活动中心共用
 */

public class PlatformIntroduceActivity extends BaseActivity {
    public static final String FLAG_INTRODUCE = "5";//平台介绍
    public static final String FLAG_ACTIVITY = "6";//活动中心


    @BindView(R.id.id_recyclerview)
    RecyclerView idRecyclerview;
    @BindView(R.id.id_multistateview)
    MultiStateView idMultistateview;
    @BindView(R.id.id_swipe)
    PullRefreshLayout idSwipe;

    private String flag;

    public static Intent newInstance(Context context, String fromFrag) {
        Intent intent = new Intent(context, PlatformIntroduceActivity.class);
        intent.putExtra("flag", fromFrag);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_platform_introduce;
    }

    @Override
    protected void initView() {
        flag = getIntent().getStringExtra("flag");

        String title = "";
        if (TextUtils.equals(flag, FLAG_INTRODUCE)) {
            title = "平台介绍";

            ToolBarManager.with(this, WMContent)
                    .setTitle(title)
                    .setBackgroundColor(R.color.toolbar_bg_grey)
                    .setTitleColor(R.color.textColor)
                    .setNavigationIcon(R.drawable.ic_nav_arrow_back);
        } else {
            title = "活动中心";

            ToolBarManager.with(this, WMContent)
                    .setTitle(title)
                    .setBackgroundColor(R.color.colorPrimary)
                    .setTitleColor(R.color.white)
                    .setNavigationIcon(R.drawable.ic_nav_arrow_white);

        }

        idSwipe.setEnabled(false);
        idRecyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        initBanner();
    }


    /**
     * @description 获取数据
     */
    private void initBanner() {

        RetrofitHome.getInstance()
                .getInternalApi()
                .getBannerList(flag)
                .compose(RxSchedulers.<PageB<BannerB>>toMain())
                .subscribe(new OnRequestListV2Listener<BannerB>(this) {
                    @Override
                    protected boolean isShowProgressBar() {
                        return false;
                    }

                    @Override
                    public void onNext(PageB<BannerB> value) {
                        super.onNext(value);
                        List<BannerB> data = value.getData();
                        if (data != null && data.size() > 0) {
                            PlatformIntroduceAdapter adapter = new PlatformIntroduceAdapter(mContext);
                            adapter.setFlag(flag);
                            adapter.setDataList(value.getData());
                            idRecyclerview.setAdapter(adapter);
                        } else {
                            idMultistateview.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        idMultistateview.setViewState(MultiStateView.VIEW_STATE_ERROR);
                    }
                });
    }

}
